#include "dwglineitem.h"
#include <QPainter>
#include <QtMath>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>

DwgLineItem::DwgLineItem() {}

QRectF DwgLineItem::boundingRect() const
{
    return shape().boundingRect();
}

void DwgLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //painter->save();
    if(option->state & QStyle::State_Selected)  // 当图元被选中后，需要显示 3 个小的特征方框
    {
        //QPen pen(Qt::DashLine);
        //painter->setPen(pen);
        //painter->drawRect(this->boundingRect());
        DrawSnapRect(painter,mStartPt);
        DrawSnapRect(painter,mEndPt);
        DrawSnapRect(painter,QPointF((mStartPt.x()+mEndPt.x())/2.0,(mStartPt.y()+mEndPt.y())/2.0));
    }
    //QPen pen(DwgObjectItem::mColor, DwgObjectItem::mLineWidth, DwgObjectItem::mPenStyle);
    QPen pen(mColor, mLineWidth, mPenStyle);
    painter->setPen(pen);
    painter->drawLine(mStartPt,mEndPt);
    //painter->restore();
}

// 专门为了显示小方框定义的绘图函数，被 paint 所调用
void DwgLineItem::DrawSnapRect(QPainter *painter,QPointF cpt)
{
    QBrush brush(Qt::blue); // 定义成蓝色，
    painter->setBrush(brush); // 带笔刷的填充
    painter->drawRect(cpt.x()-5,cpt.y()-5,10,10); // 方框宽度为 10
}

int Choice= 0;  //changept 变量是一个整型全局变量，初始值为 0，也是一种简化方法，更完善的方法是使用枚举来定义成员变量。
void DwgLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPointF curpt = event->scenePos();
        // 为了捕捉到直线的 3 个特征点，我们需要比较拾取点与这 3 个点之间的距离，这里直接输入 5 为距离
        qreal dist;
        dist = sqrt(pow((mStartPt.x()-curpt.x()),2)+pow((mStartPt.y()-curpt.y()),2));
        if(dist < 5)
        {
            Choice = 1; // 捕捉到直线的开始点
            return;
        }
        dist = sqrt(pow((mEndPt.x()-curpt.x()),2)+pow((mEndPt.y()-curpt.y()),2));
        if(dist < 5)
        {
            Choice = 2; // 捕捉到直线的终止点
            return;
        }
        QPointF cenpt((mStartPt.x()+mEndPt.x())/2.0,(mStartPt.y()+mEndPt.y())/2.0);
        dist = sqrt(pow((cenpt.x()-curpt.x()),2)+pow((cenpt.y()-curpt.y()),2));
        if(dist < 5)
        {
            Choice = 3; // 捕捉到直线的中间点
            return ;
        }
    }
    QGraphicsItem::mousePressEvent(event);
}

void DwgLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(Choice== 1)
        mStartPt = event->scenePos(); // 直线的开始点直接移动到鼠标点
    if(Choice== 2)
        mEndPt = event->scenePos(); // 直线的终止点直接移动到鼠标点
    if(Choice== 3) // 捕捉到中间点，可以平移该直线
    {
        QPointF cenpt((mStartPt.x()+mEndPt.x())/2.0,(mStartPt.y()+mEndPt.y())/2.0);
        qreal dx = event->scenePos().x()-cenpt.x();
        qreal dy = event->scenePos().y()-cenpt.y();
        mStartPt += QPointF(dx,dy);
        mEndPt += QPointF(dx,dy);
        //this->moveBy(dx,dy);
    }
    this->update();
}

QPainterPath DwgLineItem::shape() const
{
    QPainterPath path;

    qreal angle = atan((mEndPt.y()-mStartPt.y())/(mEndPt.x()-mStartPt.x()));    // 先计算出直线的倾斜弧度值 angle
    qreal angle1 = angle-M_PI*100/180.0;    // 再把这个弧度顺时针转 85 度弧度
    qreal angle2 = angle+M_PI*100/180.0;    // 逆时针转 85 度的弧度值

    QPointF p1,p2,p3,p4;    // 定义 4 个点，分别是直线包围框的 4 个角点
    p1 = mStartPt+QPointF(3*cos(angle1),3*sin(angle1));
    p2 = mStartPt+QPointF(3*cos(angle2),3*sin(angle2));
    p3 = mEndPt+QPointF(3*cos(angle2),3*sin(angle2));
    p4 = mEndPt+QPointF(3*cos(angle1),3*sin(angle1));

    // 定义 4 个点，分别是直线包围框的 4 个角点
    path.moveTo(p1);
    path.lineTo(p2);
    path.lineTo(p3);
    path.lineTo(p4);
    path.lineTo(p1);
    return path;
}
